﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using SIMS.InputOut;

namespace SIMS.Global
{
    public class MyError
    {
        public int ErrorNo { get; set; }
        public string ErrorLocation { get; set; }
        public string ErrorMessage { get; set; }
        public string ErrorStack { get; set; }


        public static void CreateSqlExceptionLog(SqlException sqlEx, string strLocation, string sqlStatment)
        {
            String[] _fileContent = new String[7];
            _fileContent[0] = "Source : " + sqlEx.Source;
            _fileContent[1] = "Date Time : " + DateTime.Now.ToString();
            _fileContent[2] = "Error Code :" + sqlEx.ErrorCode.ToString();
            _fileContent[3] = "Target Site : " + sqlEx.TargetSite.ToString();
            _fileContent[4] = "Location : " + "CE.Database.MyDatabase - " + strLocation;
            _fileContent[5] = "Message : " + sqlEx.Message;
            _fileContent[6] = "Sql Statment : " + sqlStatment;
            WriteExceptionLog("sqlEx_", _fileContent);
        }

        public static void CreateExceptionLog(Exception Ex, string strLocation)
        {
            String[] _fileContent = new String[6];
            _fileContent[0] = "Source : " + Ex.Source;
            _fileContent[1] = "Date Time : " + DateTime.Now.ToString();
            _fileContent[2] = "Stack Trace :" + Ex.StackTrace.ToString();
            _fileContent[3] = "Target Site : " + Ex.TargetSite.ToString();
            _fileContent[4] = "Location : " + "CE.Database.MyDatabase - " + strLocation;
            _fileContent[5] = "Message : " + Ex.Message;

            WriteExceptionLog("Ex_", _fileContent);
        }

        public static void CreateUnhandleLog(string strError)
        {
            String[] _fileContent = new String[2];
            _fileContent[0] = "Date Time : " + DateTime.Now.ToString();
            _fileContent[1] = "Message :" + strError;

            WriteExceptionLog("Unhandle_", _fileContent);
        }

        public static void CreateErrorLog(string prefix, string strError)
        {
            String[] _fileContent = new String[2];
            _fileContent[0] = "Date Time : " + DateTime.Now.ToString();
            _fileContent[1] = "Message :" + strError;

            WriteExceptionLog(prefix, _fileContent);
        }

        public static void WriteExceptionLog(string strPrefixFileName, string[] strException)
        {
            try
            {
                MyIO _newFile = new MyIO();
                string _filePath = AppDomain.CurrentDomain.BaseDirectory + "Exception\\";
                if (_newFile.CheckDirectory(_filePath))
                {
                    string _curMonth = DateTime.Now.Year.ToString("0000") + DateTime.Now.Month.ToString("00");

                    if (!_newFile.CheckDirectory(_filePath + _curMonth))
                    {
                        if (_newFile.CreateDirectory(_filePath, _curMonth))
                        {
                            _filePath += _curMonth + "\\";
                        }
                    }
                    else
                    {
                        _filePath += _curMonth + "\\";
                    }

                    string _curDate = DateTime.Now.ToString("dd_MMM_yyyy");

                    if (!_newFile.CheckDirectory(_filePath + _curDate))
                    {
                        if (_newFile.CreateDirectory(_filePath, _curDate))
                        {
                            _filePath += _curDate + "\\";
                        }
                    }
                    else
                    {
                        _filePath += _curDate + "\\";
                    }

                    string _fileName = strPrefixFileName + DateTime.Now.ToString("dd_MMM_yyyy_hh_mm_ss_tt", new System.Globalization.CultureInfo("en-US")) + ".txt";

                    _newFile.WriteNewFile(_filePath, _fileName, strException);
                    _newFile = null;
                }
            }
            catch (Exception Ex)
            {
                Ex = null;
            }
        }
    }


}
